//
//  SubjectView.m
//  iMarks
//
//  Created by Christian Speich on 19.01.08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "SubjectView.h"
#import "SubjectsManager.h"
#import "Mark.h"
#import "Pupil.h"
#import "Subject.h"

@implementation SubjectView

- (id)initWithSubjectIndex:(int)i andSubjectsManager:(SubjectsManager*)manager
{
    self = [super init];
    if (self) {
        if (![NSBundle loadNibNamed:@"SubjectView" owner:self]) {
			[self release];
			return nil;
		}
		index = i;
		currentHalfYear = 0;
		subjectsManager = [manager retain];
		
		[teacher setStringValue:[[subjectsManager subjectAtIndex:index] teacher]];
		[subjectName setStringValue:[[subjectsManager subjectAtIndex:index] name]];
		[average setStringValue:[NSString stringWithFormat:@"%1.2f", [[subjectsManager subjectAtIndex:index] average]]];

		[self setFrame:[subjectView frame]];
		[self addSubview:subjectView];
		[classTestsView setDelegate:self];
		[classTestsView setDataSource:self];
		[testsView setDelegate:self];
		[testsView setDataSource:self];
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_updateStatus:) name:@"MarkHasChanged" object:nil];
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_currentHalfYearHasChanged:) name:@"CurrentHalfYearHasChanged" object:nil];
    }
    return self;
}

- (BOOL)isFlipped
{
	return YES;
}

- (void)removeFromSuperview
{
	[detailViewDrawer close];
	[super removeFromSuperview];
}

#pragma mark --Actions--

- (void) markHasChanged:(id)sender
{
	[currentEditMark setMark:[sender selectedTag]+1];
	[[NSNotificationCenter defaultCenter] postNotificationName:@"MarkHasChanged" object:self userInfo:nil];
}

- (void) valencyHasChanged:(id)sender
{
	[currentEditMark setValency:([sender selectedTag]==0)?0.20f:0.25f];
	[[NSNotificationCenter defaultCenter] postNotificationName:@"MarkHasChanged" object:self userInfo:nil];
}

- (void)addOrRemoveMark:(id)sender
{
	if ([sender selectedSegment] == 0) {
		currentEditMark = [[Mark alloc] init];
		if ([detailViewDrawer state] == NSDrawerClosedState) {
			[detailViewDrawer setParentWindow:[self window]];
			[detailViewDrawer open];
		}
		[currentEditMark setType:@"test"];
		[currentEditMark setHalfYear:currentHalfYear];
		[currentEditMarkMark selectItemWithTag:[currentEditMark mark]-1];
		[detailViewDrawer setContentView:markDetailTestView];
		[[subjectsManager subjectAtIndex:index] addMark:currentEditMark];
	}
	else {
		Mark* mark = [[[subjectsManager 
						subjectAtIndex:index] marksForType:@"test" andHalfyear:currentHalfYear]
					  objectAtIndex:[testsView selectedRow]];
		[[subjectsManager subjectAtIndex:index] removeMark:mark];
		[[NSNotificationCenter defaultCenter] postNotificationName:@"MarkHasChanged" object:self userInfo:nil];
	}
}

- (void)addOrRemoveClassMark:(id)sender
{
	if ([sender selectedSegment] == 0) {
		currentEditMark = [[Mark alloc] init];
		if ([detailViewDrawer state] == NSDrawerClosedState) {
			[detailViewDrawer setParentWindow:[self window]];
			[detailViewDrawer open];
		}
		[currentEditMark setType:@"classTest"];
		[currentEditMark setValency:0.20f];
		[currentEditMark setHalfYear:currentHalfYear];
		[currentEditMarkMark selectItemWithTag:[currentEditMark mark]-1];
		[currentEditMarkValency selectItemWithTag:([currentEditMark valency]==0.20f)?0:1];
		[detailViewDrawer setContentView:markDetailClassTestView];
		[[subjectsManager subjectAtIndex:index] addMark:currentEditMark];
	}
	else {
		Mark* mark = [[[subjectsManager 
						subjectAtIndex:index] marksForType:@"classTest" andHalfyear:currentHalfYear]
					  objectAtIndex:[classTestsView selectedRow]];
		[[subjectsManager subjectAtIndex:index] removeMark:mark];
		[[NSNotificationCenter defaultCenter] postNotificationName:@"MarkHasChanged" object:self userInfo:nil];
	}
}

#pragma mark --Data Source Methods Table View--

- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
	if (aTableView == classTestsView) {
		return [[[subjectsManager subjectAtIndex:index] marksForType:@"classTest" andHalfyear:currentHalfYear] count];
	}
	else {
		return [[[subjectsManager subjectAtIndex:index] marksForType:@"test" andHalfyear:currentHalfYear] count];
	}
}

- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
			row:(int)rowIndex
{
	if (aTableView == classTestsView) {
		if ([[aTableColumn identifier] isEqualToString:@"mark"]) {
			int mark = [[[[subjectsManager subjectAtIndex:index] marksForType:@"classTest" andHalfyear:currentHalfYear] objectAtIndex:rowIndex] mark];
			
			return [NSString stringWithFormat:@"%i", mark];
		}
		else {
			float valency = [[[[subjectsManager subjectAtIndex:index] marksForType:@"classTest" andHalfyear:currentHalfYear] objectAtIndex:rowIndex] valency];
			
			return [NSString stringWithFormat:@"%i%%", (int)(valency*100)];
		}
	}
	else {
		int mark = [[[[subjectsManager subjectAtIndex:index] marksForType:@"test" andHalfyear:currentHalfYear] objectAtIndex:rowIndex] mark];
		
		return [NSString stringWithFormat:@"%i", mark];
	}
}



#pragma mark --Data Source Methods Table View--

- (BOOL)tableView:(NSTableView *)aTableView shouldEditTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
	if ([detailViewDrawer state] == NSDrawerClosedState) {
		[detailViewDrawer setParentWindow:[self window]];
		[detailViewDrawer open];
	}
	
	if (aTableView == classTestsView) {
		currentEditMark = [[[subjectsManager subjectAtIndex:index] marksForType:@"classTest" andHalfyear:currentHalfYear] objectAtIndex:rowIndex];
		[currentEditMarkMark selectItemWithTag:[currentEditMark mark]-1];
		[currentEditMarkValency selectItemWithTag:([currentEditMark valency]==0.20f)?0:1];
		[detailViewDrawer setContentView:markDetailClassTestView];
	}
	else {
		currentEditMark = [[[subjectsManager subjectAtIndex:index] marksForType:@"test" andHalfyear:currentHalfYear] objectAtIndex:rowIndex];
		[currentEditMarkMark2 selectItemWithTag:[currentEditMark mark]-1];
		[detailViewDrawer setContentView:markDetailTestView];
	}
	
	return NO;
}

#pragma mark --Notifications--

- (void)_updateStatus:(NSNotification *) notification
{
	[average setStringValue:[NSString stringWithFormat:@"%1.2f", [[subjectsManager subjectAtIndex:index] average]]];
	[classTestsView reloadData];
	[testsView reloadData];
}

- (void)_currentHalfYearHasChanged:(NSNotification *) notification
{
	if ([[notification userInfo] objectForKey:@"subjectManager"] == subjectsManager) {
		Pupil *pupil = [subjectsManager pupil];
		currentHalfYear = [pupil currentHalfYear];
		[pupil release];
		
		[average setStringValue:[NSString stringWithFormat:@"%1.2f", [[subjectsManager subjectAtIndex:index] average]]];
		[classTestsView reloadData];
		[testsView reloadData];
	}
}

- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
{
	if (([classTestsView selectedRow] == -1) && [classTestSegmentedCell isEnabledForSegment:1]) {
		[classTestSegmentedCell setEnabled:NO forSegment:1];
		[detailViewDrawer close];
	}
	else if([classTestsView selectedRow] != -1) {
		[classTestSegmentedCell setEnabled:YES forSegment:1];
	}
	
	if (([testsView selectedRow] == -1) && [testSegmentedCell isEnabledForSegment:1]) {
		[testSegmentedCell setEnabled:NO forSegment:1];
		[detailViewDrawer close];
	}
	else if([testsView selectedRow] != -1) {
		[testSegmentedCell setEnabled:YES forSegment:1];
	}
}

@end
